W razie problemów technicznych ze Szkopułem, prosimy o kontakt mailowy pod adresem [email protected].
Jeśli chciałbyś porozmawiać o zadaniach, rozwiązaniach lub problemach technicznych, zapraszamy na serwery Discord. Są one moderowane przez społeczność, ale członkowie zespołu technicznego też są tam aktywni.
The most popular sport in Byteland is BitBall. Many cities of Byteland organize BitBall Leagues every year. Each BitBall team has the right to participate in any number of leagues (no matter who is the organizer of the league). At the end of the season all the results are summarized. Each league publishes its own ranking list, which orders teams participating in that league. On the base of these ranking lists, `The best BitBaller' magazine determines and publishes the super ranking list of all BitBall teams.
Determining the super ranking list is not an easy task. The process of calculating it requires the following assumptions. If a given team took -th place on the ranking list which consists of positions, then the number of points that this team scored is equal to . If the team did not participate in the league at all, it receives points. The distance between two given ranking lists is computed as follows: for each team we calculate the absolute value of the difference between the number of points the team scored on both lists, then we calculate the sum of such values obtained for all teams. Super ranking list that has to be determined is such that its total distance from all ranking lists is minimal.
Write a program which:
In the first line of the standard input there are two integers and (), separated by a single space. They represent the number of teams and the number of leagues. Following lines describe the leagues. Description of each league consists of an integer (), representing number of teams in the league, followed by numbers (). No two numbers and for are equal. Number means that -th place in the ranking list has been taken by the team . All numbers in the lines are separated by single spaces.
In the first line of the standard output your program should write a single integer - the total distance of the super ranking list (calculated as the sum of distances from all ranking lists).
For the input data:
4 2 3 1 2 3 2 4 3
the correct result is:
11
Task author: Marcin Stefaniak.